﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models.Interfaces;

namespace GameSchool.Models.Repositories
{
    public class CommentLikeRepository : ICommentLikeRepository
    {
        GameSchoolDBDataContext db = new GameSchoolDBDataContext();

        //Returns the comments for a lecture ordered by the date ascending
       public IQueryable<Comment> GetComments(int? lectureID)
        {
            var cList = (from c in db.Comments
                         where c.LectureID == lectureID
                         select new Comment{ ID = c.ID, CommentText = c.CommentText, CommentDate = c.CommentDate });
     
            return cList;
        }

        //Checks for empty content, adds current datetime as timestamp and marks as submittable.
       public void AddComment(Comment c)
        {
            if (c.CommentText.Length == 0)
                throw new Exception("AddComment - empty comment");

            c.CommentDate = DateTime.Now;
            db.Comments.InsertOnSubmit(c);

        }
        
        //First we delete all the likes on that comment, and then the comment itself.
       public void DeleteComment(int? cID)
        {
            if (cID == null)
                throw new ArgumentNullException("Delete comment - null passed as parameter");

           var likesOnComment =   from likes in db.Likes
                                               where likes.CommentID == cID
                                               select likes;

            var c = (from com in db.Comments
                     where com.ID == cID
                     select com).SingleOrDefault();

            db.Likes.DeleteAllOnSubmit(likesOnComment);
            db.Comments.DeleteOnSubmit(c);
 
        }

        //Checks the argument and then returns the likes for the comment ordered by datetime ascending - newest first.
       public IQueryable<Like> GetLikes(int? cID)
        {
            if(cID == null)
                throw new ArgumentNullException("GetLikes - null passed as parameter");

            var likesList = from l in db.Likes
                                    where l.CommentID == cID
                                    orderby l.DateLiked ascending
                                    select l;

            return likesList;
        }

        //Timestamps the like and submits the changes
        public void AddLike(Like l)
        {
            l.DateLiked = DateTime.Now;
            db.Likes.InsertOnSubmit(l);

        }
        public void DeleteLike(int? lID)
        {
            if(lID == null)
                throw new ArgumentNullException("Delete like - null passed as parameter");

            var l = (from likes in db.Likes
                        where likes.ID == lID
                        select likes).SingleOrDefault();

            db.Likes.DeleteOnSubmit(l);
        }

        
        public void GiveCommentPoints(int? cID)
        {
            if(cID == null)
                    throw new ArgumentNullException("GiveCommentPoints - null passed as parameter");

            var c = (from comment in db.Comments
                            where comment.ID == cID
                            select comment).SingleOrDefault();

            c.GivesPoints = true;
        }
         

        public void Save()
        {
            db.SubmitChanges();
        }

    }
}